import { tauri } from '@tauri-apps/api'
import { appWindow } from '@tauri-apps/api/window'
import { StateResp, stateDefault } from './state'
import { delay } from '../utils/custom';
import { ref } from 'vue';

export let stateResp = ref(stateDefault());

appWindow.listen<StateResp>('state_resp', (env) => {
    stateResp.value = env.payload;
});

export function errMsg() {
    switch (stateResp.value.error_code) {
        case 0: return '无故障';
        case 1: return '驱动器故障';
        case 2: return '超出软限位故障';
        case 3: return '碰撞故障';
        case 4: return '奇异位姿';
        case 5: return '从站错误';
        case 6: return '指令点错误';
        case 7: return 'IO错误';
        case 8: return '夹爪错误';
        case 9: return '文件错误';
        case 10: return '参数错误';
        case 11: return '扩展轴超出软限位错误';
        case 12: return '关节配置警告';
        default: return '未知错误';
    }
}

function programState() {
    switch (stateResp.value.program_state) {
        case 1: return '停止';
        case 2: return '运行';
        case 3: return '暂停';
        case 4: return '拖动';
        default: return '未知';
    }
}

function robotMode() {
    switch (stateResp.value.robot_mode) {
        case 0: return '自动模式';
        case 1: return '手动模式';
        case 2: return '拖动模式';
        default: '未知模式';
    }
}

function emergencyStop() {
    switch (stateResp.value.emergency_stop) {
        case 0: return '无';
        case 1: return '急停';
        default: return '未知';
    }
}

function motionDone() {
    switch (stateResp.value.robot_motion_done) {
        case 1: return '到位';
        case 0: return '未到位';
        default: return '未知';
    }
}

function gripperDone() {
    switch (stateResp.value.gripper_motion_done) {
        case 0: return '未完成';
        case 1: return '完成';
    }
}

export class RobotState {
    errMsg = errMsg();
    progState = programState();
    mode = robotMode();
    emergencyStop = emergencyStop();
    robotDone = motionDone();
    gripperDone = gripperDone();
}


export async function waitGripperReady(timeout: number = 10 * 1000) {
    let time_count = 0;
    while (true) {
        await delay(150);

        if (stateResp.value.gripper_motion_done != 0) {
            break;
        }

        if (stateResp.value.error_code != 0) {
            throw `设备故障:${errMsg()}`
        }

        time_count += 150;
        if (time_count >= timeout) {
            throw `等待加载就绪超时`;
        }
    }
}

export async function waitRobotReady(timeout: number = 10 * 1000) {
    let time_count = 0;
    
    while (true) {
        await delay(150);
    
        if (stateResp.value.robot_motion_done != 0) {
            break;
        }

        if (stateResp.value.error_code != 0) {
            throw `设备故障:${errMsg()}`
        }

        time_count += 150;
        if (time_count >= timeout) {
            throw `等待机械臂就绪超时`;
        }
    }
}

export interface CtrlProto {
    cnt: number,
    cmd_code: number,
    data_len: number,
    data: string,
}

export const robot = {

    async isConnect() {
        return await tauri.invoke('is_connect') as boolean;
    },

    async connect() {
        return await tauri.invoke('connect');
    },

    async disconnect() {
        return await tauri.invoke('disconnect');
    },

    async request(cmdCode: number, name: string, args: string[]) {
        let resp = await tauri.invoke('request', {
            cmdCode: cmdCode,
            name: name,
            args: args,
        }) as CtrlProto;
    
        if (resp.data != "1") {
            throw `${name}执行失败=>${resp.data}`;
        }
        return resp;
    },
};

